package com.cuiyun97.aspect;

import com.cuiyun97.annotation.AutoFill;
import com.cuiyun97.constant.AutoFillConstant;
import com.cuiyun97.context.BaseContext;
import com.cuiyun97.enums.AutoFillType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自动填充数据表字段
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    /*
        定义在service上
        因为很多时候不需要调用mapper,
        并且要求控制器不能直接调用mp的方法,
        必须定义service
     */
    @Pointcut("execution(* com.cuiyun97.service.*.*(..)) && @annotation(com.cuiyun97.annotation.AutoFill)")
    public void autoFillPointCut() {}

    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段填充...");

        // 获取注解
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        AutoFillType operationType = autoFill.value();

        // 根据参数处理
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }

        // 约定实体对象为第1个参数
        Object entity = args[0];
        Class entityClass = entity.getClass();

        // 创建对应值
        LocalDateTime now = LocalDateTime.now();
        Integer adminId = BaseContext.getAdminId();

        if (operationType == AutoFillType.INSERT) {
            try {
                Method setCreateTime = entityClass.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entityClass.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Integer.class);
                Method setUpdateTime = entityClass.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entityClass.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Integer.class);
                setCreateTime.invoke(entity, now);
                setCreateUser.invoke(entity, adminId);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, adminId);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (operationType == AutoFillType.UPDATE) {
            try {
                Method setUpdateTime = entityClass.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entityClass.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Integer.class);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, adminId);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
